Visual Basic stores dates and times as decimal
numbers. The digits to the left of the decimal represent the number of
days since December 30, 1899, and the digits to the right of the decimal
represent the fraction of the day that has passed (for instance, 0.5 =
noon).
This means that dates and
times use the same operators as numeric types. For example, the
following expression shows yesterday's date:
Debug.Print Date - 1
This also means that you can use date or time literals to work with dates. For example, if you type #0.0# in the Code window, Visual Basic automatically changes what you typed into the time literal for midnight shown here:
dt = #12:00:00 AM#
You can edit that
literal to add a certain number of seconds, minutes, or hours to the
time. For example, the following code pauses Excel for five seconds:
Sub TakeFive( )
Dim dt As Date
' Five seconds.
dt = #12:00:05 AM#
Debug.Print "Paused..."
' Wait till five seconds from now.
Application.Wait Now + dt
Debug.Print "Resumed."
End Sub
Visual Basic provides a whole set of functions for working with dates and times, as listed in Table 1.
Table 1. Visual Basic functions for working with date and time
Category | Function | Use to |
---|
Current | Date | Get or set the system date |
| Now | Get the current date and time |
| Time | Get or set the system time |
| Timer | Get the number of seconds since midnight (often used to measure performance) |
Date | DateSerial | Convert year, month, and day numbers into a date |
| DateValue | Convert a string into a date |
| Day | Get the day of the month from a date |
| Month | Get the month of the year from a date |
| Weekday | Get the weekday from a date (1 to 7) |
| Year | Get the year from a date |
Time | Hour | Get the hour from a time |
| Minute | Get the minute from a time |
| Second | Get the second from a time |
| TimeSerial | Convert hour, minute, and second numbers into a time |
| TimeValue | Convert a string into a time |
For most conversions, the Format function works better than the functions listed in Table 1. The date/time functions are mainly used for simple operations, such as getting the current year:
Debug.Print Year(Now)
The Timer
function is very handy when developing programs since it lets you see
how long your code takes to run. When developing large or complex
programs, it is pretty common to record the Timer value at the start of the process, then display the difference between that value and the current Timer when the task completes, as shown by the following changes to DemoSort:
Sub DemoSort( )
Dim str As String, arr As Variant, d As Double
' Time this operation.
d = Timer
str = "Q z v w p x f g J l h r y D k i e T s u o n M a c b"
' Show case-sensitive sort.
Debug.Print SortString(str, False)
' Show case-insensitive sort.
Debug.Print SortString(str, True)
' Display how long the task took.
Debug.Print Timer - d
End Sub
You should use the Double data type when measuring performance since many tasks take only a fraction of a second. On my computer, DemoSort takes only about 0.00128 seconds.